package com.codi.fcloud.ufx.hssale.trade;

import com.codi.base.log.annotation.Loggable;
import com.codi.base.util.ExceptionUtil;
import com.codi.bus.constant.GlobalConstant;
import com.codi.bus.exception.HundsunAPIException;
import com.codi.fcloud.ufx.PrintUtil;
import com.codi.fcloud.ufx.UfxSDKFactory;
import com.codi.fcloud.ufx.constant.HSInputParameterNames;
import com.codi.fcloud.ufx.constant.UrlConstants;
import com.codi.trade.dto.HsRedeemTradeDto;
import com.codi.trade.service.RedeemTradeService;
import com.google.common.base.Preconditions;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

@Service("redeemTradeService")
public class RedeemTrade extends UfxSDKFactory implements RedeemTradeService {

    // 赎回转购业务在赎回接口实现，可以实现不同TA下的基金份额转换，转换同一TA请用基金转换
    @Loggable
    public Map<String, Object> sell(String capitalMode, String tradeAcco, String password, String fundCode,
                                    String shareType, BigDecimal shares, String tradeSource) {

        try {
            String trustWay = apiValueMap.get(HSInputParameterNames.TRUST_WAY);

            Map<String, Object> request = new HashMap<String, Object>();
            // 交易委托方式
            request.put("trust_way", trustWay);
            // 交易账号
            request.put("trade_acco", tradeAcco);
            // 密码
            request.put("password", password);
            // 申请时间
            request.put("apply_time", "");
            // 下单日期
            request.put("order_date", "");
            // 基金代码
            request.put("fund_code", fundCode);
            // 份额分类
            request.put("share_type", shareType);
            // 发生份额
            request.put("shares", shares);
            // 巨额赎回标志
            request.put("fund_exceed_flag", GlobalConstant.FUND_EXCEED_FLAG);
            // 资金方式
            request.put("capital_mode", capitalMode);
            // 银行代码
            request.put("bank_no", "");
            // 银行账号
            request.put("bank_account", "");
            // 经纪人
            request.put("broker", "");
            // 业务大类
            request.put("busin_board_type", "");
            // 交易来源
            request.put("trade_source", tradeSource);// 必输字段 ?
            // 交易来源申请编号
            request.put("channel_serial_id", "");
            // 对方交易账号
            request.put("oppo_trade_account", "");
            // 赎回转购业务 目标基金代码
            request.put("target_fund_code", "");
            // 赎回转购业务 目标基金类别
            request.put("target_share_type", "");
            // 业务辅助代码
            request.put("busin_ass_code", "");

            Map<String, Object> response = getUfxSDK().transact(getTarget(), request);
            PrintUtil.printResponse(response);

            return appendLog(request, response);
        } catch (Exception exception) {
            ExceptionUtil.logError(logger, getTarget(), exception);
            throw new HundsunAPIException(ExceptionUtil.getMessage(exception));
        }
    }

    @Override
    @Loggable
    public Map<String, Object> sell(HsRedeemTradeDto dto) {
        Preconditions.checkNotNull(dto.getTrade_acco());
        Preconditions.checkNotNull(dto.getPassword());
        Preconditions.checkNotNull(dto.getCapital_mode());
        Preconditions.checkNotNull(dto.getFund_code());
        Preconditions.checkNotNull(dto.getShare_type());
        Preconditions.checkNotNull(dto.getShares());

        try {
            String trustWay = apiValueMap.get(HSInputParameterNames.TRUST_WAY);

            Map<String, Object> request = new HashMap<>();
            // 交易委托方式
            request.put("trust_way", trustWay);
            // 交易账号
            request.put("trade_acco", getString(dto.getTrade_acco()));
            // 密码
            request.put("password", getString(dto.getPassword()));
            // 下单日期
            request.put("order_date", getString(dto.getOrder_date()));
            // 申请时间
            request.put("apply_time", getString(dto.getApply_time()));
            //	C6	基金代码	Y
            request.put("fund_code", getString(dto.getFund_code()));
            //	C1	份额分类	Y	A-前收费，B-后收费
            request.put("share_type", getString(dto.getShare_type()));
            //	N16.2	发生份额	Y
            request.put("shares", getString(dto.getShares()));
            //	C1	巨额赎回标志	Y	0-放弃超额部分，1-继续赎回
            request.put("fund_exceed_flag", getString(dto.getFund_exceed_flag(),GlobalConstant.FUND_EXCEED_FLAG));
            //	C1	资金方式	Y	支持1位资金方式
            request.put("capital_mode", getString(dto.getCapital_mode()));
            //	C3	银行编号	N	代销超级现金宝必填字段
            request.put("bank_no", getString(dto.getBank_no()));
            //	C28	银行账号	N	代销超级现金宝必填字段
            request.put("bank_account", getString(dto.getBank_account()));
            //	C8	经纪人	N
            request.put("broker", getString(dto.getBroker()));
            //	C2	业务大类	N	代销超级现金宝传02
            request.put("busin_board_type", getString(dto.getBusin_board_type()));
            //	C12	交易来源	N	代销超级现金宝传分配交易来源
            request.put("trade_source", getString(dto.getTrade_source()));// 必输字段 ?
            //	C32	银商流水号	N
            request.put("channel_serial_id", getString(dto.getChannel_serial_id()));
            //	C17	对方交易账号	N
            request.put("oppo_trade_account", getString(dto.getOppo_trade_account()));
            //	C6	目标基金代码	N	赎回转购必填字段
            request.put("target_fund_code", getString(dto.getTarget_fund_code()));
            //	C1	目标份额类型	N	赎回转购必填字段
            request.put("target_share_type", getString(dto.getTarget_share_type()));
            //	C2	业务辅助代码	N	赎回转申购则填06；赎回转认购则填04
            request.put("busin_ass_code", getString(dto.getBusin_ass_code()));
            //	C20	原确认编号	N
            request.put("originconfirm_no", getString(dto.getOriginconfirm_no()));
            //	N5.4	费率折扣	N
            request.put("discount_rate", getString(dto.getDiscount_rate()));


            Map<String, Object> response = getUfxSDK().transact(getTarget(), request);
            PrintUtil.printResponse(response);

            return appendLog(request, response);
        } catch (Exception exception) {
            ExceptionUtil.logError(logger, getTarget(), exception);
            throw new HundsunAPIException(ExceptionUtil.getMessage(exception));
        }
    }

    @Override
    protected String getTarget() {
        return UrlConstants.PREFIX_SALE + UrlConstants.REDEEM_TRADE;
    }

}
